﻿using UnityEngine;
using System.Collections;

public class Player : Character
{	
	public float m_PunchComboDelay = 0.25f;

	private int m_ID;
	public int ID
	{
		get { return m_ID; }
		set { m_ID = value; }
	}
	private bool m_LockDirection;
	public bool LockDirection
	{
		get { return m_LockDirection; }
		set { m_LockDirection = value; }
	}
	private bool m_LockMove;
	public bool LockMove
	{
		get { return m_LockMove; }
		set { m_LockMove = value; }
	}
	private bool m_LockFight;
	public bool LockFight
	{
		get { return m_LockFight; }
		set { m_LockFight = value; }
	}
	private bool m_LockAnimSpeed;
	public bool LockAnimSpeed
	{
		get { return m_LockAnimSpeed; }
		set { m_LockAnimSpeed = value; }
	}
	public bool LockAll
	{
		set 
		{ 
			m_LockDirection = value;
			m_LockMove = value;
			m_LockMove = value; 
		}
	}
	private bool m_IsFighting;
	private bool m_IsWalking;
	private bool m_StartCombo;
	private int m_CurrentPunch = 0;
	private float m_ComboTimer = 0;
	private float m_HorizontalDirection;
	private float m_VerticalDirection;
	private Bounds m_VisualBounds;
	
	protected override void Awake()
	{
		base.Awake ();
		RefManager.Instance.RegisterPlayer (this, 1);
	}

	private void Start () 
	{
		//TODO Assign real ID
		m_ID = 1;
		m_LockDirection = false;
		m_LockMove = false;
		m_LockFight = false;
		m_IsFighting = false;

		m_AttackColliderMessage.m_OnTriggerEnter2D += OnAttack;

		if(m_Direction == Character.Direction.LEFT)
		{
			FlipSprite();
			m_Direction = Character.Direction.RIGHT;
			m_PreviousDirection = m_Direction;
		}
	}
	
	protected override void Update () 
	{
		base.Update();

		/*if(!m_LockMove)*/ ControllerMove ();
		if(!m_LockFight) Fight ();
		CheckCombo();
	}

	private void OnDestroy()
	{
		m_AttackColliderMessage.m_OnTriggerEnter2D -= OnAttack;
	}
	
	#region CONTROLLER MOVE	
	private void ControllerMove()
	{
		m_HorizontalDirection = InputManager.GetAxis(m_ID, InputManager.Axis.HORIZONTAL);
		m_VerticalDirection = InputManager.GetAxis(m_ID, InputManager.Axis.VERTICAL);
		
		m_HighestSpeed = Mathf.Abs(m_HorizontalDirection) >  Mathf.Abs(m_VerticalDirection) ? m_HorizontalDirection : m_VerticalDirection;
		
		if(!m_LockDirection)
		{
			if(m_HorizontalDirection < 0)
			{
				m_Direction = Character.Direction.LEFT;
			}
			else if(m_HorizontalDirection > 0)
			{
				m_Direction = Character.Direction.RIGHT;
			}
		}
		
		m_IsWalking = Mathf.Abs(m_HighestSpeed) > 0;
		
		m_Animator.SetBool("Walking", m_IsWalking);
		if(!m_LockMove && !m_LockAnimSpeed) m_Animator.speed = Mathf.Abs(m_HighestSpeed) * m_AnimSpeedModifier;
		
		if(!m_LockMove)
		{
			Move (m_HorizontalDirection * m_HorizontalWalkingSpeed,
			      -m_VerticalDirection * m_VerticalWalkingSpeed);
		}
		else
		{
			Move (0, 0);
		}
		
		if(m_PreviousDirection != m_Direction)
		{
			FlipSprite();
		}

		// Set the z according to the y position for z index drawing order
		SetZIndex(m_ShadowRenderer.bounds.center.y);

		m_PreviousDirection = m_Direction;
	}
	#endregion
	
	#region FIGHT
	private void Fight()
	{
		if(!m_IsFighting)
		{
			// PUNCH
			if(InputManager.GetButtonDown(m_ID, InputManager.Button.X))
			{
				m_CurrentFightingMoveType = FightingMoveInfos.FightingMoveType.PUNCH;

				SetupFighting();

				m_Animator.SetFloat("PunchCombo", (float)m_CurrentPunch);
				m_Animator.SetTrigger("Punch");
				//m_LockMove = true;
				StartCoroutine("EndFighting" , 0.35f);
				
				if(m_CurrentPunch == 0)
				{
					m_CurrentPunch++;
				}
				else if(m_CurrentPunch == 1)
				{
					m_CurrentPunch--;
				}
				m_StartCombo = false;
			}
			// LOWKICK
			else if(InputManager.GetButtonDown(m_ID, InputManager.Button.Y))
			{
				m_CurrentFightingMoveType = FightingMoveInfos.FightingMoveType.LOWKICK;

				SetupFighting();

				m_Animator.SetTrigger("LowKick");
				m_LockMove = true;
				StartCoroutine("EndFighting" , 0.35f);
			}
		}
	}

	private void SetupFighting()
	{
		StopCoroutine("EndFighting");
		m_Animator.speed = 1f;
		m_Animator.SetBool("Walking", false);
		m_LockAnimSpeed = true;
		m_IsFighting = true;
		m_LockDirection = true;
	}

	private IEnumerator EndFighting(float i_Delay)
	{
		yield return new WaitForSeconds(i_Delay);
		
		m_LockMove = false;
		m_IsFighting = false;
		m_LockDirection = false;
		m_LockAnimSpeed = false;

		if(m_CurrentFightingMoveType == FightingMoveInfos.FightingMoveType.PUNCH)
		{
			m_ComboTimer = 0;
			m_StartCombo = true;
		}
	}

	private void CheckCombo()
	{
		if(m_StartCombo)
		{
			m_ComboTimer += Time.deltaTime;

			if(m_ComboTimer > m_PunchComboDelay)
			{
				m_ComboTimer = 0;
				m_StartCombo = false;
				m_CurrentPunch = 0;
			}
		}
	}
	#endregion

	#region COLLISION DETECTION
	private void OnAttack(Collider2D i_Collider)
	{
		//Debug.Log (i_Collider.name);
		if(i_Collider.tag == "HitDetectionEnemy")
		{
			i_Collider.GetComponent<ColliderSendMessage>().Message("OnHit", m_MovesInfo.GetMoveDamage(m_CurrentFightingMoveType));
		}
	}
	#endregion
}
